#include <iostream>

using namespace std;

const int M = 1e6 + 10,N = 1e5 + 10;

int e[N],ne[N],h,id;
int mp[M];

int main()
{
    e[1] = 1;
    mp[1] = 1;
    id++;

    int q;
    cin >> q;
    while(q--){
        int op, x, y;
        cin >> op >> x;
        int p = mp[x];
        if(op == 1){
            cin >> y;
            e[++id] = y;
            ne[id] = ne[p];
            ne[p] = id;
            mp[y] = id;
        }
        else if(op == 2){
            cout << e[ne[p]] << endl;
        }
        else{
            ne[p] = ne[ne[p]];
    }
    }
    return 0;
}